import {
	MyURLSearchParams,
	isNullOrEmpty
} from "."
import request from "@/common/request"
import useUserStore from '@/store/userStore.js'
import useConfigStore from '@/store/configStore.js'
/**
 * 分享类型
 */
export const SHARE_TYPE = {
	/**
	 * 跳转用户信息
	 */
	USER_INFO: 0,
	/**
	 * 跳转友商用户信息
	 */
	BUSSINESS_USER_INFO: 0,
	/**
	 * 跳转群聊信息
	 */
	GROUP: 1,
	/**
	 * 邀请码
	 */
	SHARE_CODE: 2
}
/**
 * 生成不同类型的分享数据体
 */
export const getShareData = () => {
	return {
		friendData: (userId) => {
			return {
				type: SHARE_TYPE.USER_INFO,
				path: '/pageSubOne/common/user-info',
				params: {
					id: userId,
					friendType: 0,
					sourceType: 0,
					fromThis: false,
					sign: new Date().getTime()
				}
			}
		},
		bussinessFriendData: (userId) => {
			return {
				type: SHARE_TYPE.BUSSINESS_USER_INFO,
				path: '/pageSubOne/common/user-info',
				params: {
					id: userId,
					friendType: 1,
					sourceType: 1,
					fromThis: false,
					sign: new Date().getTime()
				}
			}
		},
		groupData: (groupId) => {
			return {
				type: SHARE_TYPE.GROUP,
				path: '/pageSubOne/group/group-join',
				params: {
					id: groupId,
					sign: new Date().getTime()
				}
			}
		},
		shareCodeData: (shareCode) => {
			return {
				type: SHARE_TYPE.SHARE_CODE,
				path: '/pageSubOne/mine/mine-share-user',
				params: {
					shareCode: shareCode,
					sign: new Date().getTime()
				}
			}
		}
	}
}
/**
 * 生成分享二维码
 * 
 * @param {Object} body 数据,@/utils/qrcode.genShareData().xxxx
 */
export const genShareQrcode = (body) => {
	return new Promise((resolve, reject) => {
		request({
			url: `/system/qrcode`,
			method: 'POST',
			data: {
				body: JSON.stringify(body)
			}
		}).then((code) => {
			resolve(code)
		}).catch((err) => {
			reject(err)
		});
	})
}
/**
 * 处理扫码
 * @param {Object} result uni.scanCode的success返回值
 */
export const execShareData = (result) => {
	if (result.errMsg == "scanCode:ok") {
		console.log(result);
		const qrData = JSON.parse(result.result)
		console.log(qrData);
		const body = JSON.parse(qrData.body)
		console.log(body);
		// 直接跳转网页，并带预设参数
		if ([SHARE_TYPE.USER_INFO, SHARE_TYPE.BUSSINESS_USER_INFO, SHARE_TYPE.GROUP, SHARE_TYPE.SHARE_CODE]
			.includes(body.type)) {
			uni.navigateTo({
				url: body.path + "?" + new MyURLSearchParams(body.params).toString(),
			})
		}
		// 后续如果有其他的，可以在这写判断 
		/* if(body.type == 0){
			
		} else if(body.type == 1){
			
		} else if(body.type == 2){
			
		} else if(body.type == 3){
			
		} */
	} else {
		console.log("未识别到二维码")
	}
}

/**
 * 分享码操作
 */
export const initShareCode = (path) => {
	console.log(path);
	let shareCode = null
	if (path && /pageSubOne\/mine\/mine-share-user/.test(path) && path.concat(
			"shareCode")) {
		const match = path.match(/shareCode=([^&]*)/);
		shareCode = match ? match[1] : null;
	}
	if (isNullOrEmpty(shareCode)) {
		return
	}
	console.log("SHARE CODE ==>", shareCode);
	const userStore = useUserStore()
	const configStore = useConfigStore()
	// 判断有没有登录
	if (userStore.isLogin) {
		// 弹出分享码框
		configStore.showSharePanel(shareCode)
	} else {
		console.log("未登录");
		// 跳转登录
		getApp().$vm.exit();
		configStore.closeSharePanel()
	}
}